package booktest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.List;

public class BookTest {
	
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		//1.创建一个集合
		List<Book> books = new LinkedList<>();
		books.add(new Book(1, "三体"));
		books.add(new Book(2, "西游记"));
		books.add(new Book(3, "三国演义"));
		books.add(new Book(4, "水浒传"));
		books.add(new Book(5, "七龙珠"));
		
		// 2.创建文件输出字节流
		FileOutputStream fos = new FileOutputStream("books.txt");
		// 3.创建缓冲输出流
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		// 4.创建对象输出流
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		
		// 5.写列表出去
		oos.writeObject(books);
		oos.flush();
		// 6.关闭流
		oos.close();
		bos.close();
		fos.close();
		
		
		// 7. 创建文件输入字节流
		FileInputStream fis = new FileInputStream("books.txt");
		// 8. 创建缓冲输入...
		BufferedInputStream bis = new BufferedInputStream(fis);
		// 9. 对象..
		ObjectInputStream ois = new ObjectInputStream(bis);
		
		// 10.读出来
		List<Book> bs  = (List<Book>) ois.readObject();
		
		// 11.关闭
		ois.close();
		bis.close();
		fis.close();
		
		bs.forEach(System.out::println);
		
		
	}
}
